$(document).ready(function () {
    $('#navmenu ul li:not(.active)').hover(showtit, hidetit); 
	$('a#btnlogin').click(ajax_login); 
	$('#login_password').keypress(function(event){
        if ( event.which == 13 ) {
            event.preventDefault();
            ajax_login();
        }
	});
	$('a#logout').click(ajax_logout); 
	
	getSupporters(); 
});

function showtit()
{
	$(this).find('span').animate({top:'5px'},{queue:false,duration:250});
}
function hidetit()
{
	$(this).find('span').animate({top:'-20px'},{queue:false,duration:250});
}

function ajax_login()
{
	var user = $('#login_username').val();
	var pass = $('#login_password').val();
	if($.trim(user) == "" && $.trim(pass) == "")
		return false;
	var loading = "<center style='padding-top:20px;'>" + loading_img + "</center>";
	$('#box_login_content')[0].innerHTML = loading;
	$.post(base_url + "/ajax/login.aspx?user=" + user + "&pass=" + pass,
		function(data){
			if(data == "" || data.indexOf('<table id="login"') != -1)
			{
				alert("Đăng nhập thất bại.");
				$('#box_login_content')[0].innerHTML = data;
				$($('#box_login_content')[0]).css('display', 'none');
				$($('#box_login_content')[0]).fadeToggle();
				$('a#btnlogin').click(function(e){
				    e.preventDefault();
				    ajax_login();
			    });
			    $('#login_password').keypress(function(event){
                if ( event.which == 13 ) {
                    event.preventDefault();
                    ajax_login();
                }
	            });
			}
			else
			{
			    if(data.indexOf("<script type='text/javascript'>window.location.reload()</script>") != -1)
                    window.location.reload();   
				$('#box_login_content')[0].innerHTML = data;
				$($('#box_login_content')[0]).css('display', 'none');
				$($('#box_login_content')[0]).fadeToggle();
				$('a#logout').click(function(e){
					e.preventDefault();
					ajax_logout();
				}); // 3
			}
	});
	return false;
}
function ajax_logout()
{
	var loading = "<center style='padding-top:20px;'>" + loading_img + "</center>";
	$('#box_login_content')[0].innerHTML = loading;
	$.post(base_url + "/ajax/login.aspx?logout=1", function(data){
				$('#box_login_content')[0].innerHTML = data;
				$($('#box_login_content')[0]).css('display', 'none');
				$($('#box_login_content')[0]).fadeToggle()
				$('a#btnlogin').click(function(e){
				    e.preventDefault();
				    ajax_login();
			    });
			    $('#login_password').keypress(function(event){
                if ( event.which == 13 ) {
                    event.preventDefault();
                    ajax_login();
                }
	            });
	});
	return false;
}

function getSupporters()
{
    $.post(base_url + "/ajax/supporter.aspx", function(data){
		if (data.indexOf('error')!= -1)
		{
			setTimeout(getSupporters, 10000);
		}
		else
		{
			$('#box_supporter .box_content')[0].innerHTML = data;
			$($('#box_supporter .box_content')[0]).css('display', 'none');
			$($('#box_supporter .box_content')[0]).slideToggle();
		}
	});
}
function CartFunc(id, mode)
{
    var url = base_url;
    if (mode == 1)
        url += "/ajax/shoppingcart.aspx?add=" + id;
    else if (mode == 2)
        url += "/ajax/shoppingcart.aspx?empty=1";
    else if (mode == 3)
        url += "/ajax/shoppingcart.aspx?increase=" + id;
    else if (mode == 4)
    {
        if($('#qty' + id).text() == "1")
        {
            CartFunc(id, 5);
            return;
        } 
        url += "/ajax/shoppingcart.aspx?decrease=" + id
    }
    else if (mode == 5)
    {
        if(confirm("Xóa sản phẩm này khỏi giỏ hàng ?") == false)
            return false;
        url += "/ajax/shoppingcart.aspx?delete=" + id;
    }
    else if(mode == 6)
    {
        var newqty = $('#cartqty' + id).val();
        if (newqty == 0)
        {
            if(CartFunc(id, 5) == false)
                window.location.reload();
            
            return;
        }
        url += "/ajax/shoppingcart.aspx?update=" + id + "&newqty=" + newqty;
    }
    else
        return;
        
    var box = "#shoppingcart";
    $(box)[0].innerHTML = "<center><img src='" + base_url + "/images/loading.gif'/></center>";
    $.post(url, function(data){
        if(data.indexOf("<script type='text/javascript'>window.location.href='default.aspx'</script>") != -1)
            window.location.href = 'default.aspx';
        if(data.indexOf("<script type='text/javascript'>window.location.reload()</script>") != -1)
            window.location.reload();
        $(box)[0].innerHTML = data;
        $($(box)[0]).css('display', 'none');
        $($(box)[0]).fadeIn();
    });
    var box2 = "#cartdetail";
    if($(box2).length > 0)
    {
        $(box2)[0].innerHTML = "<center><img src='" + base_url + "/images/loading.gif'/></center>";
        $.post(base_url + "/ajax/cartdetail.aspx", function(data){
            $(box2)[0].innerHTML = data;
            $($(box2)[0]).css('display', 'none');
            $($(box2)[0]).slideToggle();
        });
    }
}